package com.woodblockwithoutco.remotecontrollerexample;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;

public class ViewManagerHost extends FrameLayout {
    private static final int BACKGROUND_COLOR = 0x70000000;

    private Drawable mCustomBackground;

    // This is a faster way to draw the background on devices without hardware acceleration
    private final Drawable mBackgroundDrawable = new Drawable() {
        @Override
        public void draw(Canvas canvas) {
            if (mCustomBackground != null) {
                final Rect bounds = mCustomBackground.getBounds();
                final int vWidth = getWidth();
                final int vHeight = getHeight();

                final int restore = canvas.save();
                canvas.translate(-(bounds.width() - vWidth) / 2,
                        -(bounds.height() - vHeight) / 2);
                mCustomBackground.draw(canvas);
                canvas.restoreToCount(restore);
            } else {
                canvas.drawColor(BACKGROUND_COLOR, PorterDuff.Mode.SRC);
            }
        }

        @Override
        public void setAlpha(int alpha) {
        }

        @Override
        public void setColorFilter(ColorFilter cf) {
        }

        @Override
        public int getOpacity() {
            return PixelFormat.TRANSLUCENT;
        }
    };
    
    public ViewManagerHost(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setBackgroundCompat(this, mBackgroundDrawable);
    }

    public ViewManagerHost(Context context, AttributeSet attrs) {
        super(context, attrs);
        setBackgroundCompat(this, mBackgroundDrawable);
    }

    public ViewManagerHost(Context context) {
        super(context);
        setBackgroundCompat(this, mBackgroundDrawable);
    }

    public void setCustomBackground(Drawable d) {
        mCustomBackground = d;
        if (d != null) {
            d.setColorFilter(BACKGROUND_COLOR, PorterDuff.Mode.SRC_OVER);
        }
        computeCustomBackgroundBounds();
        invalidate();
    }

    private static void setBackgroundCompat(View view, Drawable drawable) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            view.setBackground(drawable);
        } else {
            view.setBackgroundDrawable(drawable);
        }
    }

    private void computeCustomBackgroundBounds() {
        if (mCustomBackground == null) return; // Nothing to do
        if (!isLaidOut()) return; // We'll do this later

        final int bgWidth = mCustomBackground.getIntrinsicWidth();
        final int bgHeight = mCustomBackground.getIntrinsicHeight();
        final int vWidth = getWidth();
        final int vHeight = getHeight();

        final float bgAspect = (float) bgWidth / bgHeight;
        final float vAspect = (float) vWidth / vHeight;

        if (bgAspect > vAspect) {
            mCustomBackground.setBounds(0, 0, (int) (vHeight * bgAspect), vHeight);
        } else {
            mCustomBackground.setBounds(0, 0, vWidth, (int) (vWidth / bgAspect));
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        computeCustomBackgroundBounds();
    }
}
